#!/bin/bash

#
# 保留1个月日志 (delete last month log file ff.log.20230525)
#
current_date=$(date +%Y-%m-%d);

echo "$current_date";

previous_month=$(date -d "$current_date-1 month" '+%Y%m%d');

echo "$previous_month";

log_directory="/opt/te/log"

find "$log_directory" -type f -name "ff*.log.*" | while  read -r logfile ; do
    log_date=$(basename "$logfile" | grep -oP "(?<=\.log\.)\d{8}")
    echo "Find file:" "$logfile" " file suffix:" "$log_date";

  if [ -n "$log_date" ] && [ "$log_date" -lt "$previous_month" ]; then
    echo "Deleting:" "$logfile"
    rm -f "$logfile"
  fi

done
